package com.ruoyi.web.core.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.web.core.config.OssConfig;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/* loaded from: OSSUtil.class */
public class OSSUtil {
    static String endpoint        = OssConfig.END_POINT;
    static String accessKeyId     = OssConfig.ACCESS_KEY_ID;
    static String accessKeySecret = OssConfig.ACCESS_KEY_SECRET;
    static String bucketName      = OssConfig.BUCKET_NAME;

    public static Map<String, Object> Policy() {
        new SimpleDateFormat("yyyy:MM:hh").format(new Date());
        Map<String, Object> respMap = null;
        try {
            long             expireEndTime = System.currentTimeMillis() + (6000 * 1000);
            Date             expiration    = new Date(expireEndTime);
            PolicyConditions policyConds   = new PolicyConditions();
            policyConds.addConditionItem("content-length-range", 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, "key", "city/");
            OSS    ossClient     = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            String postPolicy    = ossClient.generatePostPolicy(expiration, policyConds);
            String encodedPolicy = BinaryUtil.toBase64String(postPolicy.getBytes(StandardCharsets.UTF_8));
            String postSignature = ossClient.calculatePostSignature(postPolicy);
            respMap = new LinkedHashMap<>();
            respMap.put("accessid", accessKeyId);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", "city/");
            respMap.put("host", "https://city-partner.oss-cn-shanghai.aliyuncs.com");
            respMap.put("expire", String.valueOf(expireEndTime / 1000));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return respMap;
    }

    public static String uploadFile2(MultipartFile multiFile) throws IOException {
        OSS    ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String filename  = FileUploadUtils.extractFilename(multiFile);
        try {
            try {
                filename = "city/" + filename;
                ossClient.putObject(bucketName, filename, multiFile.getInputStream());
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
            return filename;
        } catch (Throwable th) {
            if (ossClient != null) {
                ossClient.shutdown();
            }
            throw th;
        }
    }
}
